#include "sk_form_base.h"
#include "ui_sk_form_base.h"

#include "pub_var.h"
#include <QSettings>
#include <QMessageBox>
#include <QSqlError>

sk_form_base::sk_form_base(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::sk_form_base)
{
    ui->setupUi(this);

    //恢复当前窗口位置
    QSettings setIni("clin.ini", QSettings::IniFormat);
    QRect rct = setIni.value("windowState/form_base").toRect();
    this->setGeometry(rct);

    //数据库连接
    qrm = new QSqlQuery(pub::db_m);
    qrmt = new QSqlQuery(pub::db_m);
    qro = new QSqlQuery(pub::db_o);
    qrot = new QSqlQuery(pub::db_o);

    md_reg_level = new QSqlQueryModel();
    md_fee_type = new QSqlQueryModel();

    ui->tabWidget->setCurrentIndex(0);

}

sk_form_base::~sk_form_base()
{
    delete ui;
}

void sk_form_base::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/form_base", QVariant(this->geometry()));

    emit destroyW( "fin_base" );

}

void sk_form_base::on_pb_save_reg_clicked()
{
    if(ui->le_reg_name->text().trimmed().isEmpty()
            || ui->le_reg_code->text().trimmed().isEmpty()
            || ui->le_reg_ord->text().trimmed().isEmpty()
            )
    {
        QMessageBox::information(this, "注意", "基础信息不允许空缺！");
        return;
    }

    QString sq;
    sq = " select * from fin_opr_reglevel "
         " where reglevl_code = '%1' ";
    sq = sq.arg(ui->le_reg_code->text().trimmed());

    //挂号类别有效标志
    QString valid = QString::number(ui->cbb_valid->currentIndex());

    qDebug()<<"检索关键字是否已经存在："<<sq;
    qro->exec(sq);
    if(qro->next())
    {
        sq = " update fin_opr_reglevel set "
             " reglevl_name = '%1', "
             " sort_id = %2, "
             " valid_state = '%3',"
             " oper_code = '%4',"
             " oper_date = sysdate "
             " where "
             " reglevl_code = '%5'";
        sq = sq.arg(ui->le_reg_name->text().trimmed())
                .arg(ui->le_reg_ord->text().trimmed())
                .arg(valid)
                .arg(pub::usr_code)
                .arg(ui->le_reg_code->text().trimmed());

        qDebug()<<"变更挂号类别："<<sq;
        if(!qro->exec(sq))
        {
            QMessageBox::information(this, "注意", "变更类别失败！\n"
                                     + qro->lastError().text());
            qDebug()<<qro->lastError().text();
            return;
        }
    }
    else
    {
        sq = "insert into fin_opr_reglevel "
             " (reglevl_code, reglevl_name, sort_id, "
             " valid_state,oper_code,oper_date"
             " )values("
             " '%1','%2',%3,'%4','%5',sysdate) ";
        sq = sq.arg(ui->le_reg_code->text().trimmed())
                .arg(ui->le_reg_name->text().trimmed())
                .arg(ui->le_reg_ord->text().trimmed())
                .arg(valid)
                .arg(pub::usr_code);

        qDebug()<<"插入新的挂号类别："<<sq;
        if(!qro->exec(sq))
        {
            QMessageBox::information(this, "注意", "新建挂号类别失败！\n"
                                     + qro->lastError().text());
            qDebug()<<qro->lastError().text();
            return;
        }
    }

    QString diag_his = ui->le_diag_his->text().trimmed();
    QString diag_center = ui->le_diag_center->text();
    QString diag_icpc = ui->le_diag_icpc->text();

    sq = "delete from fin_opr_regfeeitem where reg_level = '%1'";
    sq = sq.arg(ui->le_reg_code->text().trimmed());
    qro->exec(sq);

    if(!diag_his.isEmpty() )
    {

        sq = "insert into fin_opr_regfeeitem"
             "(id, fee_name, fee_column_name,"
             " fee_code_his, fee_code_center,icpc_code,reg_level"
             ")values("
             "'id','%5','fee_column_name',"
             "'%1','%2','%3','%4')";
        sq = sq.arg(diag_his)
                .arg(diag_center)
                .arg(diag_icpc)
                .arg(ui->le_reg_code->text().trimmed())
                .arg(ui->le_reg_name->text().trimmed());

        qDebug()<<"插入新的挂号费用信息："<<sq;
        if(!qro->exec(sq))
        {
            QMessageBox::information(this, "注意", "保存挂号费用失败！\n"
                                     + qro->lastError().text());
            qDebug()<<qro->lastError().text();
            return;
        }
    }
    QMessageBox::information(this, "注意", "保存挂号类别完成！");

}

void sk_form_base::on_pb_save_fee_type_clicked()
{

    if(ui->le_fee_name->text().trimmed().isEmpty()
            || ui->le_fee_code->text().trimmed().isEmpty()
            || ui->le_fee_ord->text().trimmed().isEmpty()
            )
    {
        QMessageBox::information(this, "注意", "基础信息不允许空缺！");
        return;
    }

    QString sq;

    //oracle数据表

    //收费项目名称和代码都不允许重复，但变更是以代码为唯一约束标志
    sq = " select * from com_dictionary "
         " where TYPE = 'MINFEE' and (code = '%1' or name = '%2')";
    sq = sq.arg(ui->le_fee_code->text().trimmed())
            .arg(ui->le_fee_name->text().trimmed());

    //类别有效标志
    QString valid = QString::number(ui->cbb_valid_fee->currentIndex());

    qDebug()<<"检索关键字是否已经存在："<<sq;
    qro->exec(sq);
    if(qro->next())
    {
        sq = " update com_dictionary set "
             " name = '%1', "
             " sort_id = %2, "
             " valid_state = '%3',"
             " oper_code = '%4',"
             " oper_date = sysdate "
             " where "
             " TYPE = 'MINFEE'"
             " and code = '%5'";
        sq = sq.arg(ui->le_fee_name->text().trimmed())
                .arg(ui->le_fee_ord->text().trimmed())
                .arg(valid)
                .arg(pub::usr_code)
                .arg(ui->le_fee_code->text().trimmed());

        qDebug()<<"变更类别："<<sq;
        if(!qro->exec(sq))
        {
            QMessageBox::information(this, "注意", "变更类别失败！\n"
                                     + qro->lastError().text());
            qDebug()<<qro->lastError().text();
            return;
        }
    }
    else
    {
        sq = "insert into com_dictionary "
             " (code, name, sort_id, "
             " valid_state,oper_code,oper_date,"
             " TYPE, PARENT_CODE, CURRENT_CODE, IS_COMMON, MARK, SPELL_CODE"
             " )values("
             " '%1','%2',%3,'%4','%5',sysdate,"
             " 'MINFEE', 'ROOT', 'ROOT', '1', 'true', '') ";
        sq = sq.arg(ui->le_fee_code->text().trimmed())
                .arg(ui->le_fee_name->text().trimmed())
                .arg(ui->le_fee_ord->text().trimmed())
                .arg(valid)
                .arg(pub::usr_code);

        qDebug()<<"插入新的类别："<<sq;
        if(!qro->exec(sq))
        {
            QMessageBox::information(this, "注意", "新建类别失败！\n"
                                     + qro->lastError().text());
            qDebug()<<qro->lastError().text();
            return;
        }
    }


    //mysql数据表

    //收费项目名称和代码都不允许重复，但变更是以代码为唯一约束标志
    sq = " select * from com_dict_com "
         " where dic_type = 'MINFEE' and (code = '%1' or name = '%2')";
    sq = sq.arg(ui->le_fee_code->text().trimmed())
            .arg(ui->le_fee_name->text().trimmed());

    //类别有效标志
    valid = QString::number(ui->cbb_valid_fee->currentIndex());

    qDebug()<<"mysql 检索关键字是否已经存在："<<sq;
    qrm->exec(sq);
    if(qrm->next())
    {
        sq = " update com_dict_com set "
             " name = '%1', "
             " ord = %2, "
             " valid_flag = '%3',"
             " oper_code = '%4',"
             " oper_time = now() "
             " where "
             " dic_type = 'MINFEE'"
             " and code = '%5'";
        sq = sq.arg(ui->le_fee_name->text().trimmed())
                .arg(ui->le_fee_ord->text().trimmed())
                .arg(valid)
                .arg(pub::usr_code)
                .arg(ui->le_fee_code->text().trimmed());

        qDebug()<<"mysql 变更类别："<<sq;
        if(!qrm->exec(sq))
        {
            QMessageBox::information(this, "注意", "变更类别失败！\n"
                                     + qrm->lastError().text());
            qDebug()<<qrm->lastError().text();
            return;
        }
    }
    else
    {
        sq = "insert into com_dict_com "
             " (code, name, ord, valid_flag,"
             " oper_code,oper_time, dic_type, mark "
             " )values("
             " '%1','%2',%3,'%4','%5', now(), 'MINFEE', 'all') ";
        sq = sq.arg(ui->le_fee_code->text().trimmed())
                .arg(ui->le_fee_name->text().trimmed())
                .arg(ui->le_fee_ord->text().trimmed())
                .arg(valid)
                .arg(pub::usr_code);

        qDebug()<<"mysql 插入新的类别："<<sq;
        if(!qrm->exec(sq))
        {
            QMessageBox::information(this, "注意", "mysql 新建类别失败！\n"
                                     + qrm->lastError().text());
            qDebug()<<qrm->lastError().text();
            return;
        }
    }


    QMessageBox::information(this, "注意", "保存收费类别完成！");

}

void sk_form_base::on_pb_exit_clicked()
{
    close();
}

void sk_form_base::on_pb_fresh_reg_clicked()
{
    QString sq;
    sq = " select reglv.reglevl_name 挂号类别," //0
         " reglv.reglevl_code 类别代码,"//1
         " reglv.sort_id 排序 ,"//2
         " reglv.valid_state 有效标志,"//3
         " regitm.fee_code_his, "//4
         " regitm.fee_code_center,"//5
         " regitm.icpc_code, "//6
         " regitm.fee_name, "//7
         " regitm.fee_column_name "//8
         " from fin_opr_reglevel reglv"
         " left join fin_opr_regfeeitem regitm on reglv.reglevl_code = regitm.reg_level"
         " where 1 = 1 ";

    QString use_status = "'a'";
    if(ui->ckb_inuse->isChecked())
    {
        use_status += ",'1'";
    }

    if(ui->ckb_unuse->isChecked())
    {
        use_status += ",'0'";;
    }

    sq += " and valid_state in (" + use_status + ")";

    qDebug()<<"提取挂号费别："<<sq;

    md_reg_level->setQuery(sq, pub::db_o);

    ui->tv_reg_type->setModel(md_reg_level);

    int width = ui->tv_reg_type->width();

    pub::setColumnWidth(ui->tv_reg_type, "0.3^0.2^0.2^0.2^0^0^0^0^0^0^0^0", width);


}

void sk_form_base::on_tv_reg_type_clicked(const QModelIndex &index)
{
    ui->le_reg_name->setText(index.siblingAtColumn(0).data().toString());
    ui->le_reg_code->setText(index.siblingAtColumn(1).data().toString());
    ui->le_reg_ord->setText(index.siblingAtColumn(2).data().toString());
    ui->le_diag_his->setText(index.siblingAtColumn(4).data().toString());
    ui->le_diag_center->setText(index.siblingAtColumn(5).data().toString());
    ui->le_diag_icpc->setText(index.siblingAtColumn(6).data().toString());

    ui->cbb_valid->setCurrentIndex(index.siblingAtColumn(3).data().toInt());

}

void sk_form_base::on_tv_reg_type_doubleClicked(const QModelIndex &index)
{
    on_tv_reg_type_clicked(index);
}

void sk_form_base::on_tb_search_icpc_clicked()
{
    QString his_code = ui->le_diag_his->text().trimmed().toUpper();
    ui->le_diag_icpc->clear();
    ui->le_diag_center->clear();

    if(his_code.isEmpty())
    {
        QMessageBox::information(this, "注意", "检索编码不可以为空！");
        return;
    }

    QString sq = " select item_name, icpc_code,nat_code "
                 " from com_dict_icpc "
                 " where item_code = '" + his_code + "'";

    qDebug()<<"检索院内编码关联的医保码和icpc码:"<<sq;
    qrm->exec(sq);
    if(qrm->next())
    {
        ui->le_diag_icpc->setText(qrm->value("icpc_code").toString());
        ui->le_diag_center->setText(qrm->value("nat_code").toString());
    }
    else
    {
        QMessageBox::information(this, "注意", "检索编码无效！");
        return;
    }
}

void sk_form_base::on_pb_fresh_fee_clicked()
{
    QString sq;
    sq = " select name 收费类别," //0
         " code 类别代码,"//1
         " sort_id 排序 ,"//2
         " valid_state 有效标志"//3
         " from com_dictionary"
         " where TYPE = 'MINFEE' ";

    QString use_status = "'a'";
    if(ui->ckb_inuse_fee->isChecked())
    {
        use_status += ",'1'";
    }

    if(ui->ckb_unuse_fee->isChecked())
    {
        use_status += ",'0'";;
    }

    sq += " and valid_state in (" + use_status + ")";

    qDebug()<<"提取费别："<<sq;

    md_fee_type->setQuery(sq, pub::db_o);

    ui->tv_fee_type->setModel(md_fee_type);

    int width = ui->tv_fee_type->width();

    pub::setColumnWidth(ui->tv_fee_type, "0.3^0.2^0.2^0.2^0^0^0^0^0^0^0^0", width);
}

void sk_form_base::on_tv_fee_type_clicked(const QModelIndex &index)
{
    ui->le_fee_name->setText(index.siblingAtColumn(0).data().toString());
    ui->le_fee_code->setText(index.siblingAtColumn(1).data().toString());
    ui->le_fee_ord->setText(index.siblingAtColumn(2).data().toString());

    ui->cbb_valid_fee->setCurrentIndex(index.siblingAtColumn(3).data().toInt());
}

void sk_form_base::on_tv_fee_type_doubleClicked(const QModelIndex &index)
{
    on_tv_fee_type_clicked(index);
}
